package net.ojbk.saishi.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.ojbk.database.DatabaseConnection;
import net.ojbk.saishi.dao.syMatchDao;
import net.ojbk.saishi.po.syMatch;
import net.ojbk.util.*;

public class MatchBackService {
	
	public static String[] getAllSupportBasicInfoName() {
		String[] ss = {"id",
				"email",
				"tel",
				"idcard",
				"grade",
				"cokkege",
				"clas",
				"gender",
				"name",
				"nickname",
				"signature"};
		return ss;
	}
	
	public static String getOnlyMatchId() { // yes √
		DatabaseConnection dbcon = new DatabaseConnection();
		syMatchDao smd=new syMatchDao(dbcon);
		String matchId=StringTool.getRandomCapitalString(6);
		while(smd.checkOneRecord(matchId)) {  // Id 存在
			matchId=StringTool.getRandomCapitalString(6);
		}
		dbcon.close();
		return matchId;
	}
	
	public static boolean needFile(String matchId) {
		return getMatch(matchId).getNeedFile();
	}
	
	
	public static syMatch getMatch(String matchId) {
		DatabaseConnection dbcon = new DatabaseConnection();
		syMatch match = new syMatch();
		match.setMatchId(matchId);
		new syMatchDao(dbcon).queryOneRecord(match);
		dbcon.close();
		return match;
	}
	
	/**
	 * 获取 赛事介绍页面的 信息
	 * 获取的信息有 
	 * 1、赛事海报相对于 主页的相对路径
	 * 2、赛事标题
	 * 3、赛事内容
	 * @param matchId
	 * @return
	 */
	public static String[] getMatchInfo(String matchId) { // yes
		// 返回的是 字符串数组、 按上面顺序依次
		DatabaseConnection dbconn=new DatabaseConnection();
		syMatchDao smd=new syMatchDao(dbconn);
		String []strings=new String[3];
		if (!smd.checkOneRecord(matchId)) {
			strings[0] = "img" + File.separator + "callout.jpg";
			strings[1] = "所查询的赛事不存在";
			strings[2] = "所查询的赛事不存在";
			return strings;
		}
		
		syMatch sm=new syMatch();
		sm.setMatchId(matchId);
		smd.queryOneRecord(sm);
	    String picturePath= "MATCH-DATA" + File.separator + sm.getUserId() + File.separator +  matchId + File.separator +  getPosterName(sm.getUserId(),matchId);
	    List<String> stringList=new ArrayList<String>();
	    stringList.add(picturePath);
	    System.out.println("海报相对地址：\n" + picturePath);
	    stringList.add(sm.getTitle());
	    stringList.add(sm.getContent());
	    
	    stringList.toArray(strings);
	    dbconn.close();
	    return strings;
	}
	
	/**
	 * 
	 * @return 参赛信息 excel地址
	 */
	public static String getExcelPath(String userId, String matchId) { // yes
		return WebPathTool.getMatchDataPath() + userId + File.separator
				+ matchId + File.separator
				+ "StatisticalInfo.xls";
	}
	public static String getExcelPath(String matchId) {
		DatabaseConnection dbcon = new DatabaseConnection();
		syMatchDao matchDao = new syMatchDao(dbcon);
		String userId = matchDao.getUserIdByMatchId(matchId);
		dbcon.close();
		return getExcelPath(userId, matchId);
	}
	
	public static String getZipDirPath(String userId, String matchId) {
		return WebPathTool.getMatchDataPath() + userId + File.separator
				+ matchId + File.separator
				+ "treasury" + File.separator;
	}
	public static String getZipDirPath(String matchId) {
		DatabaseConnection dbcon = new DatabaseConnection();
		syMatchDao matchDao = new syMatchDao(dbcon);
		String userId = matchDao.getUserIdByMatchId(matchId);
		dbcon.close();
		return getZipDirPath(userId, matchId);
	}
	public static String getZipPath(String userId, String matchId) {
		return WebPathTool.getMatchDataPath() + userId + File.separator
				+ matchId + File.separator
				+ "treasury.zip";
	}
	public static String getZipPath(String matchId) {
		DatabaseConnection dbcon = new DatabaseConnection();
		syMatchDao matchDao = new syMatchDao(dbcon);
		String userId = matchDao.getUserIdByMatchId(matchId);
		dbcon.close();
		return getZipPath(userId, matchId);
	}
	
	/**
	 * 获取 参加赛事的文件上传 目录
	 * @param userId
	 * @param matchId
	 * @return
	 */
	public static String getTreasuryPath(String userId, String matchId) { // yes
		return getMatchRootPath(userId, matchId)
				+ "treasury" + File.separator;
	}
	
	public static String getMatchRootPath(String userId, String matchId) {
		return WebPathTool.getMatchDataPath() + userId + File.separator
				+ matchId + File.separator;
	}
	
	/**
	 * 获取 某赛事的海报名称+后缀(通过遍历赛事文件夹)
	 * @param userId 用户ID
	 * @param matchId 赛事ID
	 * @return
	 */
	public static String getPosterName(String userId, String matchId) { // yes
		String path = getMatchRootPath(userId, matchId); // 某赛事根文件夹
		//String path="F:/test1";
		File sourceFile =new File(path);
		File[] sourceFiles=sourceFile.listFiles();
		for(int i=0;sourceFiles!=null && i<sourceFiles.length;i++) {
			String fileName=sourceFiles[i].getName();
			if(fileName.endsWith(".jpg")||fileName.endsWith("png")||fileName.endsWith("gif")||fileName.endsWith("bmp")) {
				String picturePath=fileName;
				return picturePath;
			}
		}
		return null;
	}
	
	/**
	 * 返回 某赛事 的统计信息的 json 字符串
	 * @param matchId
	 * @return
	 */
	public static String getStatisticsJson(String matchId) { // no
		
		return null;
	}
	
	/**
	 * 描述：获取全部赛事，用于查看所有赛事
	 * @return
	 */
	public static syMatch[] getAllMatch() {   //yes
		DatabaseConnection dbcon = new DatabaseConnection();
		syMatchDao symd=new syMatchDao(dbcon);
		syMatch sym[];
		sym=symd.queryAllRecod();
		dbcon.close();
		return sym;
	}

	
	public static void main(String args[]) {
//		MatchBackService mbs=new MatchBackService();
//		String path=mbs.getPosterName("000", "000");
//		System.out.print(path);
//		mbs.getMatchInfo("YOIDND");
//		syMatch[] sm;
//		sm=mbs.getAllMatch();
//		System.out.println(sm.length);
	}
}
